package com.fz.us.oa.web.controller.common;


import com.fz.us.admin.base.bean.SystemConfig;
import com.fz.us.oa.core.entity.common.FileManage;
import com.fz.us.oa.core.service.common.FileManagerService;
import com.fz.us.oa.web.controller.BaseController;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 控制器 - 字典
 * ============================================================================
 * 版权所有 2016 。
 *
 * @author fallenpanda
 * @version 1.0 2016-09-14 。
 * ============================================================================
 */
@Controller
@RequestMapping(value = "/common")
public class UeditorController extends BaseController {


    private static final long serialVersionUID = -3692053559044392868L;
    private static final String[] ACTION_FILE = {"uploadimage", "uploadscrawl", "uploadvideo", "uploadfile"};
    private static final String[] ACTION_BASE64 = {"uploadscrawl"};


    @Autowired
    private SystemConfig systemConfig;
    @Autowired
    private FileManagerService fileManagerService;

    @RequestMapping(value = "ueditor", produces = MediaType.APPLICATION_JSON_VALUE)
    public Object ueditor(String action,
                          String callback,
                          @RequestParam(value = "upfile", required = false) CommonsMultipartFile file,
                          @RequestParam(value = "content", required = false) String base64) {
        if (StringUtils.equals("config", action)) {
            try {
                ResourceLoader resourceLoader = new DefaultResourceLoader();
                Resource resource = resourceLoader.getResource("classpath:/ueditor/config.json");
                if (resource.exists()) {
                    return ajaxJson(IOUtils.toString(resource.getInputStream(), "UTF-8"));
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        } else if (ArrayUtils.contains(ACTION_FILE, action)) {// 上传 图片/视频/附件
            if (file != null) {
                try {
                    FileManage fileManage = fileManagerService.gridFSSave(file.getInputStream(), file.getOriginalFilename(), file.getContentType());
                    if (fileManage != null) {
                        Map<String, Object> data = new HashMap<String, Object>();
                        data.put("state", "SUCCESS");
                        data.put("url", "http://"+systemConfig.getHost()+"/"+systemConfig.getWebroot()+"/common/file/ueditorDownload/"+fileManage.getId());
                        data.put("title", fileManage.getName());
                        data.put("original", fileManage.getName());
                        return ajaxJson(data);
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
//        else if (ArrayUtils.contains(ACTION_BASE64, action)) {// 上传 涂鸦
//            try {
//                if (StringUtils.isNotEmpty(base64)) {
//                    byte[] result = Encodes.decodeBase64(base64);
//
//
//                }
//                FileManage fileManage = fileManageService.saveGridFS(file.getInputStream(), file.getOriginalFilename(), file.getContentType(), file.getSize());
//                if (fileManage != null) {
//                    Map<String, Object> data = new HashMap<String, Object>();
//                    data.put("state", "SUCCESS");
//                    data.put("url", "../common/file/download/"+fileManage.getId());
//                    data.put("title", fileManage.getName());
//                    data.put("original", fileManage.getName());
//                    return ajaxJson(data);
//                }
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }

        Map<String, Object> data = new HashMap<String, Object>();
        data.put("state", "错误的请求");
        return ajaxJson(data);
    }

}